package com.ifreshwing.activity.base;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;

import com.ifreshwing.ainterface.Ipermissons;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lz on 2017/5/3.
 */

public class BaseFragment extends Fragment{
    Ipermissons ps_callback;
    //用户同意权限标识
    public static final int U_ALLOW = PackageManager.PERMISSION_GRANTED;
    //拒绝
    public static final int U_DENIED = PackageManager.PERMISSION_DENIED;
    /**
     * 已知权限列表
     * 如果申请的权限为当前权限集合等级为最高，那么只需要申请一个最高权限即可
     */
    //GPS权限
    public static final String P_GPS = Manifest.permission.ACCESS_FINE_LOCATION;
    //相机
    public static final String P_CAMERA = Manifest.permission.CAMERA;
    //内外置SD卡读写权限
    public static final String P_SD = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    //拨打电话权限
    public static final String P_CALL = Manifest.permission.CALL_PHONE;
    //读取手机信息
    public static final String P_READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 100:
                if (grantResults.length > 0 && grantResults[0] == U_ALLOW) {
                    ps_callback.p_CallBack(permissions[0], 1);
                } else {
                    ps_callback.p_CallBack(permissions[0], -1);
                }
                break;

            case 200:
                List<String> a_permissions = new ArrayList<>();
                List<String> d_permissions = new ArrayList<>();
                for (int i = 0; i < grantResults.length; i++) {
                    if (grantResults[i] == 0) {
                        a_permissions.add(permissions[i]);
                    } else {
                        d_permissions.add(permissions[i]);
                    }
                }
                if (a_permissions.size() == permissions.length) {
                    ps_callback.ps_CallBack(permissions, 1);
                } else {
                    String[] str_permissions = new String[d_permissions.size()];
                    d_permissions.toArray(str_permissions);
                    ps_callback.ps_CallBack(str_permissions, -1);
                }
                break;
        }

    }

    // 申请单个权限
    public void ApplyPermisson(String permissionName, Ipermissons callBack) {
        this.ps_callback = callBack;
        try {
            if (ActivityCompat.checkSelfPermission(getActivity(), permissionName) == U_DENIED) {
                ActivityCompat.requestPermissions(getActivity(), new String[]{permissionName}, 100);
            } else if (ActivityCompat.checkSelfPermission(getActivity(), permissionName) == U_ALLOW) {
                ps_callback.p_CallBack(permissionName, 0);
            } else {
                ps_callback.p_CallBack(permissionName, -1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //申请权限组
    public void ApplyPermissons(String[] permissonNames, Ipermissons callBack) {
        this.ps_callback = callBack;
        try {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < permissonNames.length; i++) {
                if ((ActivityCompat.checkSelfPermission(getActivity(), permissonNames[i]) == U_DENIED)) {
                    list.add(permissonNames[i]);
                }
            }
            if (list.size() > 0) {
                String[] rPermissons = new String[list.size()];
                list.toArray(rPermissons);
                ActivityCompat.requestPermissions(getActivity(), rPermissons, 200);
            } else ps_callback.ps_CallBack(permissonNames, 0);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
